import express from 'express';
import Sequelize from 'sequelize';
import { Favorite } from '../../modal/index'; // 数据库实体类
import { statusList, sequelizeStatus } from '../../common/status'; // 状态码对照
import { checkPermission, aesEncrypt } from '../../utils/utils'; // 工具类

const router = express.Router();
const minLevel = 0; // 允许调用接口的最低等级

router.post('/', async function (Request, Response, next) {
  // Favorite .Modal.sync();
  let param = Request.body
  // 权限校验
  await checkPermission(Request, Response, minLevel).then(res => {
    param.userId = res.id
  })
  Favorite.Modal.findAndCountAll({
    where: {
      userId: param.userId,
    },
    offset: (param.pageNo - 1) * param.pageSize,
    limit: param.pageSize, // 分页参数
    order: Sequelize.literal('createdAt DESC'), // 排序
  }).then(res => {
    Response.json({
      data: {
        list: res.rows,
        total: res.count,
      },
      message: "success"
    });
  }).catch(err => {
    console.log(err)
    Response.status(500).send({ message: sequelizeStatus[err.name] || statusList[500] || err.name });
  });
});

module.exports = router;